<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="拼团分类">
              <a-select :default-value="null" v-model="queryParam.categoryId" placeholder="请选择拼团分类"
                        @change="searchQuery">
                <a-select-option :value="null">
                  全部
                </a-select-option>
                <a-select-option v-for="(item,index) in categoryList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="活动状态">
              <a-select :default-value="null" v-model="queryParam.activeStatus" placeholder="请选择活动状态"
                        @change="searchQuery">
                <a-select-option :value="null">
                  全部
                </a-select-option>
                <a-select-option :value="1">
                  未开始
                </a-select-option>
                <a-select-option :value="2">
                  进行中
                </a-select-option>
                <a-select-option :value="3">
                  已结束
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商家名称">
              <a-input placeholder="请输入商家名称" v-model="queryParam.shopName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="12" :lg="7" :md="8" :sm="24">
            <a-form-item label="活动时间">
              <a-range-picker format="YYYY-MM-DD" :placeholder="['开始时间', '结束时间']" @change="onDateCustChange"/>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="name!='B2B2C'" @click="handleAdd1" type="primary" icon="plus">新增自营商品</a-button>
      <a-divider v-if="sysMode==3||sysMode==4" type="vertical"/>
      <a-button v-if="sysMode==3||sysMode==4" @click="handleAdd2" type="primary" icon="plus">新增店铺商品</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <!--      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
              <a style="margin-left: 24px" @click="onClearSelected">清空</a>
              :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            </div>-->

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading" class="j-table-force-wrap"
               @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="longText" slot-scope="text">
          <div style="width:180px;white-space: normal;word-break: break-all;word-wrap: break-word;line-height:16px;">
            {{ text }}
          </div>
        </template>

        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="55px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="activeStatus" slot-scope="text, record">
          <a-tag v-if="record.activeStatus==1" color="orange">
            未开始
          </a-tag>
          <a-tag v-if="record.activeStatus==2" color="green">
            进行中
          </a-tag>
          <a-tag v-if="record.activeStatus==3" color="red">
            已结束
          </a-tag>
          <!--          <div v-if="record.activeStatus==1">未开始</div>
                    <div v-if="record.activeStatus==2">进行中</div>
                    <div v-if="record.activeStatus==3">已结束</div>-->
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.activeStatus==1" @click="handleEdit(record)">编辑</a>
          <br v-if="record.activeStatus==1"/>
          <a v-if="record.isCommission == 1 && record.activeStatus==1" @click="discountEdit(record)">分佣配置</a>
          <a-divider v-if="record.isCommission == 1 && record.activeStatus==1" type="vertical"/>
          <a @click="handleDetail(record)">查看</a>
          <br/>
          <a-popconfirm v-if="record.activeStatus!=3&&record.isExpire==0" title="确定禁用吗?"
                        @confirm="() => handleStop(record.id)">
            <a>禁用</a>
          </a-popconfirm>
          <a v-else-if="record.isExpire==1" style="color:#999999;">已禁用</a>
          <a v-else style="color:#999999;">已过期</a>
          <br/>
          <a-popconfirm v-if="record.activeStatus!=2||record.isExpire==1" title="确定删除吗?"
                        @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <ptms-good-modal ref="modalForm" @ok="modalFormOk"></ptms-good-modal>
    <gms-range-info ref="rangeDiscount"></gms-range-info>
  </a-card>
</template>

<script>
import {putAction, getAction, postAction} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import PtmsGoodModal from './modules/PtmsGoodModal'
import JDate from '@/components/jeecg/JDate.vue'
import GmsRangeInfo from '@/views/gospel/mallBase/good/modules/GmsRangeInfo'
import GmsGoodRecommendedMapRef from "@views/gospel/mallBase/good/modules/GmsGoodRecommendedMapRef";

export default {
  name: 'PtmsGoodList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate,
    PtmsGoodModal,
    GmsRangeInfo
  },
  data() {
    return {
      name: process.env.VUE_APP_PATTERN,
      description: '拼团商品管理页面',
      // 表头
      columns: [
        // {
        //   title: '#',
        //   dataIndex: '',
        //   key: 'rowIndex',
        //   width: 60,
        //   align: "center",
        //   customRender: function (t, r, index) {
        //     return parseInt(index) + 1
        //   }
        // },
        {
          title: '商品编码',
          align: "center",
          dataIndex: 'itemCode'
        },
        {
          title: '拼团分类',
          align: "center",
          dataIndex: 'categoryName'
        },
        {
          title: '商品图片',
          align: "center",
          dataIndex: 'imgUrl',
          scopedSlots: {customRender: 'imgSlot'}
        },
        {
          title: '商品名称',
          align: "left",
          dataIndex: 'title',
          scopedSlots: {customRender: 'longText'}
        },
        {
          title: '商家名称',
          align: "center",
          dataIndex: 'shopName'
        },
        {
          title: '开始时间',
          align: "center",
          dataIndex: 'startTime',
          width: 100,
        },
        {
          title: '结束时间',
          align: "center",
          dataIndex: 'endTime',
          width: 100,
        },
        {
          title: '活动状态',
          align: "center",
          dataIndex: 'isActive',
          scopedSlots: {customRender: 'activeStatus'}
        },
        {
          title: '实付金额',
          align: "center",
          dataIndex: 'payAmount',
          sorter: true,
          customRender: function (text) {
            return text ? text : 0
          }
        },
        {
          title: '付款订单数',
          align: "center",
          sorter: true,
          dataIndex: 'payOrderNum'
        },
        {
          title: '付款人数',
          align: "center",
          sorter: true,
          dataIndex: 'payUserNum'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 90,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/mallPlus/ptmsGood/list",
        cateList: "/mallPlus/ptmsCategory/list",
        delete: "/mallPlus/ptmsGood/delete",
        deleteBatch: "/mallPlus/ptmsGood/deleteBatch",
        exportXlsUrl: "/mallPlus/ptmsGood/exportXls",
        importExcelUrl: "mallPlus/ptmsGood/importExcel",

      },
      dictOptions: {},
      categoryList: []//拼团分类列表
    }
  },
  created() {
    this.loadCateData()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    onDateCustChange: function (value, dateString) {
      this.queryParam.endTime_begin = dateString[0]
      this.queryParam.startTime_end = dateString[1]
    },
    loadCateData() {
      this.loading = true
      getAction(this.url.cateList, {column: 'createTime', order: 'desc', pageNo: 1, pageSize: 1000}).then((res) => {
        if (res.success) {
          this.categoryList = res.result.records
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleAdd1: function () {
      this.$refs.modalForm.add(1)
      this.$refs.modalForm.title = "新增自营商品"
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd2: function () {
      this.$refs.modalForm.add(2)
      this.$refs.modalForm.title = "新增店铺商品"
      this.$refs.modalForm.disableSubmit = false
    },
    discountEdit(record) {
      let self = this
      self.$refs.rangeDiscount.visible = true;
      self.$refs.rangeDiscount.itemId = record.id;
      self.$refs.rangeDiscount.itemType = 3;
      self.$refs.rangeDiscount.open()
    },
    //禁用
    handleStop: function (id) {
      var that = this
      putAction("/mallPlus/ptmsGood/stop", {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
